印菱形 Python 3


for i in range(-4,5,2):
    print(('*'*(5-abs(i))).center(5))

使用center函數少考慮空格問題,比較容易。
可以先把想要的解畫在座標平面上,畫完會是一個折線圖
想像成在求整數解
5 - | x | = y

for i in range(5):
    if i <3:
        print('{}{}'.format(' '*abs(2-i),'*'*(1+2*i)))
    else:
        print('{}{}'.format(' '*abs(2-i),'*'*(9-2*i)))

較麻煩,考慮空格後還要再考慮星星

for i in range(-4,5,2):
    print(' '*abs(i//2)+('*'*(5-abs(i))))

function

def drawDiamond(x):
    for i in range(-x+1,x,2):
        print(('*'*(x-abs(i))).center(x))






你可能感興趣的文章

== 跟 === 的差別

== 跟 === 的差別

Don’t break the Web:以 SmooshGate 以及 keygen 為例

Don’t break the Web:以 SmooshGate 以及 keygen 為例

給自己看的 JS 進階-物件導向

給自己看的 JS 進階-物件導向






留言討論